package com.android.takagism.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.takagism.R;
import com.android.takagism.utils.MyContext;


public class LinkActivity extends AppCompatActivity implements View.OnClickListener {
    private MyContext myContext = MyContext.getInstance();
    private EditText ip_address;
    private EditText port;
    private Button link_btn;
    private Button exit_btn;
    private String defaultIp = "192.168.43.211";
    private String defaultPort = "8000";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_link);

        myContext.setStatics("ip", defaultIp);
        myContext.setStatics("port", defaultPort);
        init();

    }

    private void init() {
        ip_address = findViewById(R.id.ip_address);
        ip_address.setText(defaultIp);
        port = findViewById(R.id.port);
        port.setText(defaultPort);
        link_btn = findViewById(R.id.link_button);
        link_btn.setOnClickListener(this);
        exit_btn = findViewById(R.id.exit_button);
        exit_btn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.link_button:
                if (!ip_address.getText().toString().equals("")) {
                    if (!port.getText().toString().equals("")) {
                        if (!defaultIp.equalsIgnoreCase(ip_address.getText().toString()))
                            myContext.setStatics("ip", ip_address.getText().toString());
                        if (!defaultPort.equalsIgnoreCase(port.getText().toString()))
                            myContext.setStatics("port", port.getText().toString());

                        boolean isConnected = NetworkDetector.detect(LinkActivity.this);
                        if (!isConnected) {
                            Toast.makeText(LinkActivity.this, "网络不可用!", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(LinkActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(LinkActivity.this, AdminActivity.class));
                        }
                    }
                    System.out.println("link");
                }
                break;
            case R.id.exit_button:
                finish();
                break;
            default:
                break;
        }
    }

    public static class NetworkDetector {
        public final static int NONE = 0;
        // 无网络
        public final static int WIFI = 1;
        // Wi-Fi
        public final static int MOBILE = 2;

        public static boolean detect(Activity act) {
            ConnectivityManager manager = (ConnectivityManager) act.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

            if (manager == null) {
                return false;
            }

            NetworkInfo networkinfo = manager.getActiveNetworkInfo();

            return networkinfo != null && networkinfo.isAvailable();
        }
    }
}